export function dateFormat(date) {
  let ret;
  let fmt = "YYYY-mm-dd HH:MM:SS";
  const opt = {
    "Y+": date.getFullYear().toString(),
    "m+": (date.getMonth() + 1).toString(),
    "d+": date.getDate().toString(),
    "H+": date.getHours().toString(),
    "M+": date.getMinutes().toString(),
    "S+": date.getSeconds().toString(),
  };
  for (let key in opt) {
    ret = new RegExp("(" + key + ")").exec(fmt);
    if (ret) {
      fmt = fmt.replace(
        ret[1],
        ret[1].length == 1 ? opt[key] : opt[key].padStart(ret[1].length, "0")
      );
    }
  }
  return fmt;
}

export function dateFormatPattern(fmt, date) {
  let ret;
  const opt = {
    "Y+": date.getFullYear().toString(),
    "m+": (date.getMonth() + 1).toString(),
    "d+": date.getDate().toString(),
    "H+": date.getHours().toString(),
    "M+": date.getMinutes().toString(),
    "S+": date.getSeconds().toString(),
  };
  for (let key in opt) {
    ret = new RegExp("(" + key + ")").exec(fmt);
    if (ret) {
      fmt = fmt.replace(
        ret[1],
        ret[1].length == 1 ? opt[key] : opt[key].padStart(ret[1].length, "0")
      );
    }
  }
  return fmt;
}
